/***************************************
 2010-5-5
 ****************************************/
package com.happy.services;

import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocCollector;
import org.apache.lucene.search.TopFieldDocCollector;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.core.orm.hibernate.HibernateDao;
import com.core.orm.hibernate.Page;
import com.happy.entity.Ariticle;
import com.happy.exception.ServicesException;

@Service
@Transactional
public class AriticleServices {
	/** 默认无参构造函数 **/
	public AriticleServices() {
	}

	private HibernateDao<Ariticle, Long> ariticleDao;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		ariticleDao = new HibernateDao<Ariticle, Long>(sessionFactory,
				Ariticle.class);

	}

	public Page<Ariticle> findByPage(Page<Ariticle> page)
			throws ServicesException {
		
		return ariticleDao.findPage(page, "from  Ariticle where status=1");
	}

	public Page<Ariticle> search(String args,Page<Ariticle> page) {
		FullTextSession fulltxt = Search.createFullTextSession(ariticleDao
				.getSession());
		MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] {
				"title", "content" }, new StandardAnalyzer());
		try {
			Query query = fulltxt.createFullTextQuery(parser.parse(args),
					Ariticle.class);
			page.setTotalCount(query.list().size());
			query.setFirstResult((page.getPageNo()-1)*page.getPageSize());
			query.setMaxResults(page.getPageSize());
			
			page.setResult(query.list());
			return page;
		} catch (ParseException e) {
			System.out.println("出现异常信息"+e.getMessage());
		} 
		return null;
	}

	public Ariticle get(long id) throws ServicesException {
		
		return ariticleDao.get(id);
	}

	public void save(Ariticle ar) throws ServicesException {
		ariticleDao.save(ar);
	}

}
